#!/bin/sh

# Lua compile wrapper-script for 'compile.sh'.
# See that script for syntax and more info.
#
# This script does not actually "compile" the source, but writes a
# shell script that will function as the executable: when called, it
# will execute the source with the correct interpreter syntax, thus
# allowing this interpreted source to be used transparantly as if it
# was compiled to a standalone binary.
#
# This script requires that lua is installed in the chroot.

DEST="$1" ; shift
MAINSOURCE="$1"

# Check lua syntax:
for i in "$@" ; do
	luac -p "$i"
	EXITCODE=$?
	[ "$EXITCODE" -ne 0 ] && exit $EXITCODE
done

# Pre-compile to byte-code for faster loading and forcing that all
# files compile together:
luac -o luac.out "$@"

# Write executing script:
cat > "$DEST" <<EOF
#!/bin/sh
# Generated shell-script to execute lua interpreter on source.
# Detect dirname and change dir to prevent class not found errors.
if [ "\${0%/*}" != "\$0" ]; then
    cd "\${0%/*}"
fi

exec lua luac.out
EOF

chmod a+x "$DEST"

exit 0